const app = getApp();
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad() {
    // 查看是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']){
          wx.getUserInfo({
            success(res){
              wx.redirectTo({
                url: '../index/index'
              })
            }
          })
        }
      }
    })
  },
  bindGetUserInfo(e){
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success(a){
              var str = JSON.parse(a.rawData);
              wx.login({
                success(res){
                  if (res.code){
                    app.util.request({
                      url: 'admin/WxGoodsApi/wxUser',
                      data:{
                        code: res.code,
                        nickName: str['nickName'],
                        avatarUrl: str['avatarUrl'],
                        city: str['city'],
                        country: str['country'],
                        gender: str['gender'],
                        language: str['language']
                      },
                      method: 'POST',
                      header:{
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      success: function(res){
                        console.log(res)
                        // wx.setStorageSync('access_token', res.data.data.access_token)
                        // wx.setStorageSync('openid', res.data.data.openid)
                        // wx.setStorage({
                        //   key: 'access_token',
                        //   data: res.data.data.access_token
                        // })
                        wx.setStorage({
                          key: 'openid',
                          data: res.data.data.openid
                        })
                        wx.redirectTo({
                          url: '../index/index'
                        })
                      }
                    });
                  } else {
                    console.log('登录失败！' + res.errMsg)
                  }
                }
              })
            }
          })
        }
      }
    })
  }
})